package com.example.es2helloworldpl3;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private final Random gerador = new Random();
	private int valor;
	private EditText txtGuess;
	private Button btnGuess;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		valor = geraValorAleatorio(1, 10);

		txtGuess = (EditText) findViewById(R.id.txtGuess);
		btnGuess = (Button) findViewById(R.id.btnGuess);

		btnGuess.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (txtGuess.getText() == null || txtGuess.getText().toString().trim().isEmpty())
					return;
				int guess = Integer.parseInt(txtGuess.getText().toString());
				if (guess == valor)
					Toast.makeText(getApplicationContext(), "Acertou!",
							Toast.LENGTH_SHORT).show();
				else if (guess < valor)
					Toast.makeText(getApplicationContext(), "para cima...",
							Toast.LENGTH_SHORT).show();
				else
					Toast.makeText(getApplicationContext(), "para baixo",
							Toast.LENGTH_SHORT).show();
				txtGuess.setText("");

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


	private int geraValorAleatorio(int min, int max){
		int valor = gerador.nextInt(max - min + 1);
		valor = min + valor;
		return valor;
	}

}
